<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Module Model
 *
 * @since      Version 0.1
 * @filesource
 */
class Module_Model extends HYD_Model {

    public function __construct() {
        parent::__construct();
        $this->set_table_name('module');
    }

    /**
     * 保存数据包括插入、更新
     *
     * 更新或插入数据,$id为NULL时插入，反之更新
     * @param	string id ID
     * @param	array $data 数据
     * @return	integer  1=成功   -1=pv4错误 -2=pv6错误 -3=pv4存在 -4=pv6存在   -5=名已存在 MODEL_RETURN_DBERROR=数据库错误
     */
    public function save($id = NULL, $data) {
        $feild_array = array('id_value' => $id);

        //检查名称是否重复
        $feild_array['where'] = array('fd_name' => $data['fd_name']);
        if ($this->get_is_duplicate($feild_array)) {
            return -5;
        }
        //检查ip是否重复
        if ($data['fd_ipv4'] != '') {
            $feild_array['where'] = array('fd_ipv4' => $data['fd_ipv4']);
            if ($this->get_is_duplicate($feild_array)) {
                return -3;
            }
        }
        if ($data['fd_ipv6'] != '') {
            $feild_array['where'] = array('fd_ipv6' => $data['fd_ipv6']);
            if ($this->get_is_duplicate($feild_array)) {
                return -4;
            }
        }
        //事务开始
        $this->db->trans_begin();
        $row_data = $this->get_update_feilds($data);

        if (is_numeric($id)) {
            $this->db->update($this->get_table_name(), $row_data, array('fd_id' => $id));
        } else {
            $this->db->insert($this->get_table_name(), $row_data);
        }
        if ($this->check_dbtrans() === FALSE) {
            return MODEL_RETURN_DBERROR;
        }
        //事务结束
        $this->db->trans_commit();
        return MODEL_RETURN_SUCCESS;
    }
    /**
     * 根据名字取信息
     * @param type $fd_name
     * @return type
     */
    public function get_data_name($fd_name) {
        $feild_list = $this->get_all_feilds();
        $result_data = $this->db
                ->select($feild_list)
                ->from($this->get_table_name())
                ->where('fd_name', $fd_name)
                ->where('fd_id>', 0)
                ->get();

        return $result_data->row_array();
    }
    /**
     * 删除数据记录
     *
     * 删除指定一条数据记录
     * @param	 string id ID
     * @return	 bool  TRUE=成功 -500=失败
     * 
     * @todo 删除组织机构记录
     */
    public function delete($id) {
        $this->db->trans_begin();
        $this->db->delete($this->get_table_name(), array('fd_id' => $id));
        if ($this->check_dbtrans() === FALSE) {
            return MODEL_RETURN_DBERROR;
        }
        return MODEL_RETURN_SUCCESS;
    }

    /**
     * 获取数据列表
     *
     * 根据分页条件获取数据列表
     * @param	string $start 记录开始位置
     * @param	string $limit 记录数
     * @return	 array 结果集
     */
    public function get_lists($start = NULL, $limit = NULL, $sql_str = '') {
        $feild_list = $this->get_all_feilds();
        $this->db->select($feild_list);
        $this->db->from($this->get_table_name());
        $where_str = 'fd_id>0';
        //搜索处理      
        if (strlen($sql_str) > 0) {
            if ($where_str) {
                $where_str .= " and ( {$sql_str} )";
            } else {
                $where_str = $sql_str;
            }
        }

        if ($where_str) {
            $this->db->where($where_str, NULL, FALSE);
        }
        $this->db->order_by('fd_name');
        if (!(is_null($start) && is_null($limit))) {
            $this->db->limit($limit, $start);
        }
        $result_list = $this->db->get();
        return $result_list->result_array();
    }

    /**
     * 获取记录总数
     * @return	 integer 
     */
    public function get_total() {
        return $this->db->count_all();
    }

    /**
     * 获取数据记录
     *
     * 根据ID获取指定一条数据记录
     * @param	 string id 组织机构ID
     * 
     * @return	 array 结果集
     */
    public function get_data($id) {
        $feild_list = $this->get_all_feilds();
        $result_data = $this->db
                ->select($feild_list)
                ->from($this->get_table_name())
                ->where('fd_id', $id)
                //->where('fd_id>', 0)
                ->get();
        return $result_data->row_array();
    }

    /**
     * 取由节点“id、名称”组成的数组用于显示select选单
     * @return array
     */
    public function get_select_arr($fid = NULL,$order_by = NULL) {        
        $this->db->select('id, name, description as text ');
        $this->db->from($this->get_table_name());

        if (!is_null($fid)) {
            $this->db->where('fid', $fid);
        }
        if (!is_null($order_by)) {
            $this->db->order_by($order_by);
        }
        $this->db->order_by('sortid');
        $result_list = $this->db->get();
        return $result_list->result_array();
    }
    
    /**
     * 获取节点其它属性
     *
     * 根据id获取节点其它属性
     * @param	 string $node_id  组织机构ID
     * 
     * @return	 array 结果集
     */
    public function get_properties($node_id, $fd_key = NULL) {
        $feild_list = $this->get_all_feilds('sys_node_properties');
        $where['fd_node_id'] = $node_id;
        if (!is_null($fd_key)) {
            $where['fd_key'] = $fd_key;
        }
        $result_data = $this->db
                ->select($feild_list)
                ->from('sys_node_properties')
                ->where($where)
                ->get();
        if (!is_null($fd_key)) {
            return $result_data->row_array();
        } else {
            return $result_data->result_array();
        }       
    }
    
    /**
     * 更新节点属性表
     * @param	 integer $node_id 节点ID
     * @param   array $data
     * @return	 bool  MODEL_RETURN_SUCCESS =成功 MODEL_RETURN_DBERROR=失败
     */
    public function set_properties($node_id, $data) {
        $this->db->trans_begin();
        foreach ($data as $row) {
            $exist_arr = $this->get_properties($node_id, $row['fd_key']);
            if ($exist_arr) {
                $this->db->update('sys_node_properties', 
                        array('fd_value'      => $row['fd_value']), 
                        array('fd_node_id'    => $node_id, 
                              'fd_category'   => $row['fd_category'], 
                              'fd_key'        => $row['fd_key']) );
            } else {//插入
                $row_data = array('fd_node_id'  => $node_id,
                                  'fd_key'      => $row['fd_key'],
                                  'fd_value'    => $row['fd_value'],
                                  'fd_category' => $row['fd_category']);
                $this->db->insert('sys_node_properties', $row_data);
            }
            if ($this->check_dbtrans() === FALSE) {
                return MODEL_RETURN_DBERROR;
            }
        }
        //事务结束
        $this->db->trans_commit();
        return MODEL_RETURN_SUCCESS;
    }    
    
    public function get_node_org_arr($show_type = 'arr', $node_expand = TRUE) {
        $node_rows = $this->get_select_arr();
        $this->load->model('organization/organization_model');
        $tid = 1;
        $res = array();
        foreach ($node_rows as $row) {  
            $id = $row['id'];
            if ($show_type == 'arr') {
                $id .= '_'.($tid++);
            }           
            $data = array('id' => $id,
                           'text' => $row['text'],
                           'expanded' => $node_expand,
                           'leaf' => FALSE
                        );
            $org_arr = $this->organization_model->get_data_where('fd_nodeid='.$row['id'], array('fd_id', 'fd_name'));
            if ($org_arr) {
                $children = array();
                foreach ($org_arr as $org) {
                    $id = $org['fd_id'];
                    if ($show_type == 'arr') {
                        $id .= '_'.($tid++);
                    }                     
                    $children[] = array('id' => $id,
                                        'text' => $org['fd_name'],
                                        'leaf' => TRUE
                                    );
                }
                $data['children'] = $children;
                unset($children);
            } else {
                $data['leaf'] = TRUE;
            }
            $res[] = $data;
            unset($data);
        }  
        return $res;
    }
}

/* End of file nodelist_model.php */
/* Location: ./pbxadmin/models/nodelist/nodelist_model.php */